import socket
from flask import Flask, request, jsonify

app = Flask(__name__)

class DatabaseManager:
    def __init__(self, host, port):
        self.__sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        try:
            self.__sock.connect((host, port))
        except socket.error as e:
            print(f"Error connecting to database: {e}")
            raise

    def send_request(self, request):
        try:
            self.__sock.send(request.encode('utf-8'))
            self.__result = ''
            while True:
                content = self.__sock.recv(1024)
                if not content:
                    break
                self.__result += content.decode('utf-8')
            return self.__result
        except Exception as e:
            print(f"Error sending request to database: {e}")
            raise

    def close(self):
        self.__sock.close()

# 创建 DatabaseManager 类的实例
db_manager = DatabaseManager('192.168.159.129', 2000)

@app.route('/user', methods=['GET'])
def deal_http_request():
    input_id = request.args.get('input_id')
    if input_id == 'all':
        result = db_manager.send_request('get_all')
    else:
        result = db_manager.send_request(f'get {input_id}')
    return jsonify({'result': result})

if __name__ == '__main__':
    app.run()


http://192.168.159.131/user/input_id=all 